import { useState, useEffect } from "react";
import { Form, message } from "antd";
import { CommonModal } from "@/views/components/CommonModal";
import StashSelect from "@/views/components/StashSelect";
import { setProductinboundorderEntry } from "../service";

const SetIncomingWarehouseModal = ({ visible, checkedRows, fetchList, handleCancel, warehouseList, filteredArray }) => {
	const [form] = Form.useForm();
	const [selectedWarehouse, setSelectedWarehouse] = useState(null);
	const [selectedWarehouseName, setSelectedWarehouseName] = useState(null);
	const [warehouseType, setWarehouseType] = useState(null);
	useEffect(() => {}, [visible]);
	const onOk = async () => {
		const list = filteredArray.map(x => x.inboundOrderNo);
		const { success } = await setProductinboundorderEntry({
			inboundOrderNoList: list,
			warehouseType: warehouseType,
			warehouseId: selectedWarehouse,
			warehouseName: selectedWarehouseName
		});
		if (success) {
			message.success("操作成功");
			onCancel();
			fetchList();
		}
	};
	const onCancel = () => {
		handleCancel();
		form.resetFields();
	};
	const handleWarehouseChange = value => {
		setSelectedWarehouse(value.value);
		setSelectedWarehouseName(value.label);
		setWarehouseType(value.title);
	};

	return (
		<CommonModal
			title={{ title: "设置入仓仓库" }}
			className="views-table-dialog"
			visible={true}
			onOk={onOk}
			onCancel={onCancel}
			width="494px"
			minHeight="150px"
		>
			<Form>
				<Form.Item label="" name="status3">
					<StashSelect
						value={selectedWarehouse}
						placeholder={"选择入库仓库"}
						warehouseList={warehouseList.current}
						onChange={handleWarehouseChange}
					/>
				</Form.Item>
			</Form>
		</CommonModal>
	);
};

export default SetIncomingWarehouseModal;
